package cn.gs.global.server.account;

// @author gs
import cn.gs.db.HibernateUtil;
import cn.gs.exchanger.ExchangerClient;
import cn.gs.exchanger.ExchangerException;
import cn.gs.exchanger.java.JavaExchangerClient;
import cn.gs.global.common.account.IAccountServer;
import cn.gs.global.common.meta.IObjServer;
import cn.gs.global.common.meta.IOgnServer;
import cn.gs.global.common.timer.TimerClientTool;
import cn.gs.global.table.account.SimOgn;
import cn.gs.util.set.Cache;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AccountCore {

    public static void main(String[] args) throws UnknownHostException, IOException, ExchangerException {
        ExecutorService pool = Executors.newSingleThreadExecutor();
        JavaExchangerClient client = new JavaExchangerClient("127.0.0.1", ExchangerClient.PORT);
        AccountCore core = new AccountCore(pool);
        core.registerService(client);
    }

    public AccountCore(ExecutorService pool) {
        this.pool = pool;
        if (DEBUGGING) {
            util = new HibernateUtil("gsaccount.test.cfg.xml");
        } else {
            util = new HibernateUtil("gsaccount.cfg.xml");
        }
        caches = new Cache<>(30 * 60 * 1000, 100);
    }
    public final static boolean DEBUGGING = Boolean.valueOf(System.getProperty("cn.gs.test"));
    private final ExecutorService pool;
    private final HibernateUtil util;
    private TimerListenerImpl impl;
    private Cache<Integer, MonthAccount.ObjInfoSplit> caches;
    private IOgnServer ognsvr;
    private IObjServer objsvr;

    public MonthAccount create(SimOgn ogn) {
        return new MonthAccount(caches, ognsvr, objsvr, util, ogn);
    }

    public void registerService(JavaExchangerClient client) throws ExchangerException {
        client.register(new AccountServer(this), IAccountServer.class);
        impl = new TimerListenerImpl(this);
        TimerClientTool.register(client, impl, "AccountServer" + UUID.randomUUID().toString().substring(30));
        ognsvr = client.bind(IOgnServer.class);
        objsvr = client.bind(IObjServer.class);
    }

    public int date() {
        return impl.date;
    }

    public HibernateUtil getUtil() {
        return util;
    }

    public ExecutorService getPool() {
        return pool;
    }
}
